/*
 * @lc app=leetcode.cn id=740 lang=golang
 *
 * [740] 删除并获得点数
 */

package Solutions

import (
	"sort"
)

// @lc code=start
func deleteAndEarn(nums []int) int {
	sumMp := make(map[int]int, 1<<4)
	for _, v := range nums {
		sumMp[v] += v
	}
	nums = make([]int, 0, len(sumMp))
	for k := range sumMp {
		nums = append(nums, k)
	}
	sort.Ints(nums)
	var L1, L2 int
	L2 = sumMp[nums[0]]
	if len(nums) == 1 {
		return L2
	}
	if nums[1]-nums[0] > 1 {
		L1 = sumMp[nums[0]] + sumMp[nums[1]]
	} else {
		if sumMp[nums[0]] > sumMp[nums[1]] {
			L1 = sumMp[nums[0]]
		} else {
			L1 = sumMp[nums[1]]
		}
	}
	for i := 2; i < len(nums); i++ {
		v := nums[i]
		if v > nums[i-1]+1 {
			L2 = L1
			L1 += sumMp[v]
		} else {
			if L2+sumMp[v] > L1 {
				t := L1
				L1 = L2 + sumMp[v]
				L2 = t
			} else {
				L2 = L1
			}
		}
	}

	return L1
}

// func main() {
// 	data := []int{3, 4, 2}
// 	fmt.Println(deleteAndEarn(data))
// }

// @lc code=end
